﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace MedReg.Classes
{
    [Serializable]
    [Table (Name = "Post")]
    public class Post : ISerializable
    {
        private long idField;

        private long parentField;

        private string nameField;

//        private EntitySet<StaffEntry> _staffentry = new EntitySet<StaffEntry>();

        private static readonly Post theOneObject = new Post();

        public static Post GetPost()
        {
            return theOneObject;
        }
        /// <remarks/>
/*        
        [Association(Name = "Post_StaffEntry_FK1", Storage = "_staffentry", ThisKey = "ID", OtherKey = "StuffPostId")]
        public EntitySet<StaffEntry> StaffEntries
        {
            get
            {
                return _staffentry;
            }
            set
            {
                _staffentry.Assign(value);
            }
        }
        
*/
        
        [Column (IsPrimaryKey = true)]
        public long ID
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <remarks/>
      //  [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        [Column]
        public long Parent
        {
            get
            {
                return this.parentField;
            }
            set
            {
                this.parentField = value;
            }
        }

        /// <remarks/>
        [Column]
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //throw new NotImplementedException();
            info.AddValue("ID", idField);
            info.AddValue("Parent", parentField);
            info.AddValue("Name",nameField);
        }
        /*
        public Post (SerializationInfo info, StreamingContext context)
        {
            idField = (int) info.GetValue("ID", typeof (int));
            parentField = (int?) info.GetValue("Parent", typeof (int?));
            nameField = (string)info.GetValue("Name", typeof(string));
        }
         
        public  Post (int ID, int? Parent, string Name)
        {
            this.idField = ID;
            this.parentField = Parent;
            this.nameField = Name;
        }
        */
        public  Post ()
        {
        }
        public List<Classes.Post> getPostList(Classes.ArrayOfStaffEnrty staffEntryCollection)
        {

            var sPost = new List<Classes.Post>(from po in staffEntryCollection select po.StuffPost);

            var ssSub = sPost
                .GroupBy(s => new { s.ID })
                .Select(g => g.First())
                .OrderBy(o => o.ID)
                .ToList();
            return ssSub;
        }
        public List<Classes.Post> getPostList(List<Classes.CardRecord> sCardRecord)
        {

            var sPost = new List<Classes.Post>(from lv in sCardRecord select lv.RecordPost);

            var ssSub = sPost
                .GroupBy(s => new { s.ID })
                //.GroupBy(s => new { s.ID, s.Name, s.Parent })
                .Select(g => g.First())
                .OrderBy(o => o.ID)
                .ToList();
            return ssSub;
        }

        /*
        public void ImportList(List<Classes.Post> postList)
        {
            var mdb = new Medreg();
            Post query;
            foreach (var post in postList)
            {
                Post post1 = post;
               
                if (mdb.Post.LongCount() == 0)
                {
                    mdb.Post.InsertOnSubmit(post1);
                }
                else
                {
                 try
                    {
                        query = mdb.Post.Single(l => l.ID == post1.ID);
                        query.Name = post1.Name;
                        query.Parent = post1.Parent;
                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {
                        mdb.Post.InsertOnSubmit(post1);
                    }
                }
            }
            mdb.SubmitChanges();
        }
        */
    }
    
}
